package com.thinkfit.jy.utils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.common.lib.manager.DataManager;
import com.thinkfit.jy.R;

/**
 * 加载弹窗
 */
public class LoadViewUtils implements View.OnClickListener {

    private static LoadViewUtils loadViewUtils;
    WindowManager windowManager;
    Context mContext;
    boolean isFist = true;
    View inflate;
    TextView tvStr;

    int num = 0;

    public static LoadViewUtils getInstance(){
        if(loadViewUtils == null){
            loadViewUtils = new LoadViewUtils();
        }
        return loadViewUtils;
    }

    public void initView(Context context,int strId){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_load, null);
            tvStr = inflate.findViewById(R.id.tv_str);
            tvStr.setOnClickListener(this);
        }
        if(strId != 0){
            tvStr.setText(context.getString(strId));
        }

//        tvCount.setText(time+"");
//        startCountDown(time);

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        layoutParams.gravity = Gravity.CENTER;

        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            inflate.setVisibility(View.VISIBLE);
        }

    }
    Toast toast;
    public void showToast(){
//        mContext = context;
//        if(!isShowToast){
//            return;
//        }
        if(mContext == null){
            return;
        }
        if(toast == null){
            toast= new Toast(mContext);
            View inflater = LayoutInflater.from(mContext).inflate(R.layout.item_toast, null);
            TextView tv_toast = inflater.findViewById(R.id.tv_toast);
            if(!DataManager.Companion.getInstance().isLogin()){
                tv_toast.setText(mContext.getString(R.string.app_toast_hint_5));
            }else {
                tv_toast.setText(mContext.getString(R.string.app_toast_hint_4));
            }

            toast.setGravity(Gravity.CENTER , 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(inflater);
        }
        toast.show();

        goneLayout();
    }

    public void setHint(int idStr){
        if (inflate!=null && inflate.getVisibility() == View.VISIBLE && tvStr!=null){
            tvStr.setText(mContext.getString(idStr));
        }
    }

    public void goneLayout(){
        if(windowManager != null){
            if(inflate != null){
                inflate.setVisibility(View.GONE);
            }
        }

    }
    public void invaliView(){
        if(inflate != null){
            windowManager.removeView(inflate);
            isFist = true;
            windowManager = null;
            inflate = null;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_str:
                num++;
                if(num == 5){
                    num = 0;
                    goneLayout();
                }
                break;
        }
    }

    public void setContext(Context context){
        mContext = context;
    }
}
